package com.gdpu.project.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.project.service.TeacherService;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String teacherId;//这里的username是编号
	private String password;
	private int role;
	private HttpServletRequest request;
	private HttpSession session;
	private TeacherService teacherService;

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	/**
	 * 登录窗口验证
	 */
	public String execute() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		Boolean flag=false;
		/*if(this.getTeacherId()==null){
			String teacherId=request.getParameter("teacherId");
			this.setTeacherId(teacherId);
		}*/
		flag = teacherService.checkTeacherLogin(this.getTeacherId(),
				this.getPassword(), this.getRole());
		
		
		if (flag) {//表该用户存在
			Teacher teacher = teacherService.findTeacherById(this.getTeacherId());//根据Id查找
			if(this.getRole()==0){
				session.setAttribute("username", teacher.getName());
				session.setAttribute("teacherId", teacher.getTeacherId());
				session.setAttribute("role","教职工");//0对应的是教职工
				session.setAttribute("teacher", teacher);
				return "teacher";
			}else if(this.getRole()==1){//1对应的是院办领导
				session.setAttribute("username", teacher.getName());
				session.setAttribute("role","院办领导");
				session.setAttribute("teacherId", teacher.getTeacherId());
				session.setAttribute("teacher", teacher);
				return "admin";
			}else{
				request.setAttribute("message", "请选择角色");
				return "failed";
			}
		} else {
			request.setAttribute("message", "用户名或密码不正确！");
			return "failed";
		}
	}

}
